﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using BaocaoNPC.common;
using BaocaoNPC.Usercontrols.reports;
namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for Nhap_KHTTTT.xaml
    /// </summary>
    public partial class Nhap_KHTTTT : UserControl
    {
        public DataSet ds_KHTTTT = new DataSet();
        public String p_madvi = NPCUS_DanhMuc.Pglobal.strMa_dviqly.ToString(); //CMISLibrary.Libraries.UserInfomation.MaDViqly.ToString();
        public Nhap_KHTTTT()
        {
            InitializeComponent();
            try
            {
                GlobalServices.Service_NPCUS.CapnhatdsloTT_KH(p_madvi);
                loadgrid();
                btnGhi.IsEnabled = false;
                btnHuy.IsEnabled = false;
                btnNhap.IsEnabled = true;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void btnNhap_Click(object sender, RoutedEventArgs e)
        {
            btnGhi.IsEnabled = true;
            btnHuy.IsEnabled = true;
            btnNhap.IsEnabled = false;
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnGhi.IsEnabled = false;
                btnHuy.IsEnabled = false;
                btnNhap.IsEnabled = true;
                loadgrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ds_KHTTTT != null)
                {
                    if (ds_KHTTTT.Tables.Count > 0)
                    {
                       //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                       //if (bus.update_khtttt(ds_KHTTTT))
                        if (GlobalServices.Service_NPCUS.update_khtttt(ds_KHTTTT))
                        //if(bus.update_khttcc(ds_KHTRAMCC))
                        {
                            MessageBox.Show("Cập nhật số liệu thành công");
                            //load lai grid
                            //---load lai grid
                            loadgrid();

                            btnGhi.IsEnabled = false;
                            btnHuy.IsEnabled = false;
                            btnNhap.IsEnabled = true;
                        }
                        else { MessageBox.Show("Lỗi trong quá trình cập nhật"); }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void loadgrid()
        {
            try
            {
                ds_KHTTTT.Clear();
                ds_KHTTTT = GlobalServices.Service_NPCUS.Load_KHOACHLOTT(p_madvi);
                if (ds_KHTTTT != null)
                {
                    if (ds_KHTTTT.Tables.Count > 0)
                    {
                        //ds_KHTTTT.Tables[0].TableName = "KHTTTT";
                        dgr_KHTTTT.ItemsSource = ds_KHTTTT.Tables["KHLOTT"].DefaultView;
                        ds_KHTTTT.Tables["KHLOTT"].DefaultView.AllowNew = false;
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
